package io.github.biezhi.elves.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 日期工具类
 *
 */
public class DateUtils {
	//定义多种日期格式化类型
		public static final String dateformat = "yyyyMMdd";
		public static final String dateformat1 = "yyyyMM";
		public static final String dateformat2 = "yyyy";
		public static final String dateformat3 = "MM-dd-yyyy";
		public static final String dateformat4 = "yyyyMMddHHmmss";
		public static final String dateformat5 = "yyyy-MM-dd HH:mm:ss";
		public static final String dateformat6 = "yyyy-MM-dd-mm-ss";
		public static final String dateformat7 = "EEEE-MMMM-dd-yyyy";
		public static final String dateformat8 = "yyyy-MM-dd";
		public static final String dateformat9 = "MM月";
		public static final String dateformat10 = "MM.dd";
		public static final String dateformat11 = "dd";
		public static final String dateformat12 = "MM月dd日";
		public static final String dateformat13 = "MM";
		public static final String dateformat14 = "yyyy年MM月dd日";
		public static final String dateformat15 = "yyyyMMddHH";
		public static final String dateformat16 = "yyyyMMddHHmmss";

		/**
		 * 把Date转换成String，以yyyy-MM-dd HH:mm:ss的形式显示
		 * 
		 * @param tempDate
		 * @return
		 */
		public static String dateToStringFormat(String tempDateStr) {
			String date_str = "";
			SimpleDateFormat formatter = new SimpleDateFormat(dateformat5); // 规定日期格式
			try {
				Date tempDate = stringToDate(tempDateStr);
				date_str = formatter.format(tempDate);
			} catch (Exception ex) {
				date_str = "";
			}
			return date_str;
		}

		/**
		 * 把Date转换成String，以自定义的形式显示
		 * 
		 * @param tempDate
		 * @param dateFormat
		 * @return
		 */
		public static String dateToStringFormat(String tempDateStr, String dateFormat) {
			String date_str = "";
			SimpleDateFormat formatter = new SimpleDateFormat(dateFormat); // 规定日期格式
			try {
				Date tempDate = stringToDate(tempDateStr, dateFormat);
				date_str = formatter.format(tempDate);
			} catch (Exception ex) {
				date_str = "";
			}
			return date_str;
		}

		/**
		 * 把Date转换成String，以自定义的形式显示
		 * 
		 * @param tempDate
		 * @param dateFormat1
		 * @param dateFormat2
		 * @return
		 */
		public static String dateToStringFormat(String tempDateStr, String dateFormat1, String dateFormat2) {
			String date_str = "";
			try {
				date_str = dateToString(stringToDate(tempDateStr, dateFormat1), dateFormat2);
			} catch (Exception ex) {
				date_str = "";
			}
			return date_str;
		}
		
		/**
		 * 时间戳转换成日期格式字符串
		 * 
		 * @param timestamp	时间戳
		 * @param format		格式化
		 * @return
		 */
		public static String timestampToDate(Long timestamp, String format) {
			String seconds = String.valueOf(timestamp/1000);
			if (StringUtils.isNullOrEmpty(seconds) || seconds.equals("null")) {
				return "";
			}
			if (format == null || format.isEmpty()) {
				format = "yyyy-MM-dd HH:mm:ss";
			}
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.format(new Date(Long.valueOf(seconds + "000")));
		}
		
		/**
		 * 日期格式字符串转换成时间戳
		 * 
		 * @param dateStr 	字符串日期
		 * @return
		 */
		public static Long dateToTimestamp(String dateStr) {
			SimpleDateFormat formatter = new SimpleDateFormat(dateformat5); // 规定日期格式
			Long timestamp = 0L;
			try {
				// 先转换成long型
				timestamp = formatter.parse(dateStr).getTime();
			} catch (Exception ex) {
			}
			return timestamp;
		}

		/**
		 * 把Date转换成String，以yyyy-MM-dd HH:mm:ss的形式显示
		 * @param tempDate
		 * @return
		 */
		public static String dateToString(Date tempDate) {
			String date_str = "";
			SimpleDateFormat formatter = new SimpleDateFormat(dateformat5); // 规定日期格式
			try {
				date_str = formatter.format(tempDate);
			} catch (Exception ex) {
				date_str = "";
			}
			return date_str;
		}

		/**
		 * 把Date转换成String，以自定义的形式显示
		 * @param tempDate
		 * @param dateFormat
		 * @return
		 */
		public static String dateToString(Date tempDate, String dateFormat) {
			String date_str = "";
			// 规定日期格式
			SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
			try {
				date_str = formatter.format(tempDate);
			} catch (Exception ex) {
				date_str = "";
			}
			return date_str;
		}
		
		/**
		 * 把String转换成Date，形式为yyyy-MM-dd HH:mm:ss
		 * @param dateStr
		 * @return
		 */
		public static Date stringToDate(String dateStr) {
			SimpleDateFormat formatter = new SimpleDateFormat(dateformat5); // 规定日期格式
			long long_temp = 0;
			Date tempDate;
			try {
				// 先转换成long型
				long_temp = formatter.parse(dateStr).getTime();
				// 在转换成Date型
				tempDate = new Date(long_temp);
			} catch (Exception ex) {
				tempDate = null;
			}
			return tempDate;
		}

		/**
		 * 把String转换成Date，形式自定义
		 * @param dateStr
		 * @param dateFormat
		 * @return
		 */
		public static Date stringToDate(String dateStr, String dateFormat) {
			SimpleDateFormat formatter = new SimpleDateFormat(dateFormat); // 规定日期格式
			long long_temp = 0;
			Date tempDate;
			try {
				long_temp = formatter.parse(dateStr).getTime();// 先转换成long型
				tempDate = new Date(long_temp); // 在转换成Date型
			} catch (Exception ex) {
				tempDate = null;
			}
			return tempDate;
		}

		/**
		 * 取前某天时间
		 * 
		 * @param dateStr			参数说明:时间/日期字符串
		 * @param dateFormat		参数说明:定义时间/日期指定格式
		 * @param beforeDay		参数说明:定义要取前天(例如:2017-06-18,beforeDay为1，则2017-06-17)
		 * @return
		 */
		public static String beforedateToString(String dateStr, String dateFormat, int beforeDay) {
			String dayBefore = "";
			try {
				Calendar c = Calendar.getInstance();
				c.setTime(stringToDate(dateStr, dateFormat));
				int day = c.get(Calendar.DATE);
				c.set(Calendar.DATE, day - beforeDay);
				dayBefore = new SimpleDateFormat(dateFormat).format(c.getTime());
			} catch (Exception ex) {
				dayBefore = "";
			}
			return dayBefore;
		}

		/**
		 * 把Date格式化，以自定义的形式显示
		 * 
		 * @param tempDate
		 * @param dateFormat
		 * @return
		 */
		public static Date dateFormat(Date date, String dateFormat) {
			Date tempDate;
			long long_temp = 0;
			SimpleDateFormat formatter = new SimpleDateFormat(dateFormat); // 规定日期格式
			try {
				String dateStr = dateToString(date, dateFormat);
				long_temp = formatter.parse(dateStr).getTime();// 先转换成long型
				tempDate = new Date(long_temp); // 在转换成Date型
			} catch (Exception ex) {
				tempDate = null;
			}
			return tempDate;
		}

		/**
		 * 把Date转换成Long，以自定义的形式显示
		 * 
		 * @param tempDate
		 * @param dateFormat
		 * @return
		 */
		public long dateToLongFormat(Date date, String dateFormat) {
			long long_temp = 0;
			SimpleDateFormat formatter = new SimpleDateFormat(dateFormat); // 规定日期格式
			try {
				String dateStr = dateToString(date, dateFormat);
				long_temp = formatter.parse(dateStr).getTime();
			} catch (Exception ex) {
				long_temp = 0;
			}
			return long_temp;
		}

		public static Date FUTURE_OF_FUTURE = null;
		static {
			Calendar c = Calendar.getInstance();
			c.set(4000, 1, 1, 0, 0, 0);
			FUTURE_OF_FUTURE = c.getTime();
		}

		/**
		 * 获得以前的日期 jndi
		 * 
		 * @return
		 */
		public static String getOlderday(int oldDate) {
			Date date = new Date();
			date = new Date(date.getTime() - oldDate * 1000 * 60 * 60 * 24);
			SimpleDateFormat dateFm = new SimpleDateFormat(dateformat8);
			String oldDay = "";
			try {
				oldDay = dateFm.format(date) + " 00:00:00";
			} catch (Exception e) {
				return "";
			}
			return oldDay;
		}

		/**
		 * 把Date转换成String，以yyyy-MM-dd的形式显示
		 * @param tempDate
		 * @return
		 */
		public static String dateToStringtoDay(Date tempDate) {
			String date_str = "";
			SimpleDateFormat formatter = new SimpleDateFormat(dateformat8); // 规定日期格式
			try {
				date_str = formatter.format(tempDate) + " 23:59:59";
			} catch (Exception ex) {
				date_str = "";
			}
			return date_str;
		}

		/**
		 * 给出一个表示日期的字符串，返回一个日期类型的object
		 * 
		 * @param src		表示日期的字串
		 * @param df		字串对应的日期格式
		 * @return Date
		 */
		public static Date getDateByString(String src, DateFormat df) {
			try {
				return df.parse(src);
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}

		/**
		 * 获取当天日期
		 * 
		 * @return
		 */
		@SuppressWarnings("static-access")
		public static String getPreMonthDay() {
			String dateString = "";
			try {
				// 取时间
				Date date = new Date();
				Calendar calendar = new GregorianCalendar();
				calendar.setTime(date);
				calendar.add(calendar.MONTH, -1);
				// 把日期往后增加一天.整数往后推,负数往前移动
				calendar.add(calendar.DATE, 0);
				// 这个时间就是日期往后推一天的结果
				date = calendar.getTime();
				SimpleDateFormat formatter = new SimpleDateFormat(dateformat8);
				dateString = formatter.format(date);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return dateString;
		}

		/**
		 * 获取当天日期
		 * 
		 * @return
		 */
		@SuppressWarnings("static-access")
		public static String getCurrentDay() {
			String dateString = "";
			try {
				// 取时间
				Date date = new Date();
				Calendar calendar = new GregorianCalendar();
				calendar.setTime(date);
				// 把日期往后增加一天.整数往后推,负数往前移动
				calendar.add(calendar.DATE, 0);
				// 这个时间就是日期往后推一天的结果
				date = calendar.getTime();
				SimpleDateFormat formatter = new SimpleDateFormat(dateformat8);
				dateString = formatter.format(date);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return dateString;
		}

		/**
		 * 获取当天日期
		 * 
		 * @return
		 */
		@SuppressWarnings("static-access")
		public static String getNextMonthDay() {
			String dateString = "";
			try {
				// 取时间
				Date date = new Date();
				Calendar calendar = new GregorianCalendar();
				calendar.setTime(date);
				calendar.add(calendar.MONTH, +1);
				// 把日期往后增加一天.整数往后推,负数往前移动
				calendar.add(calendar.DATE, 0);
				// 这个时间就是日期往后推一天的结果
				date = calendar.getTime();
				SimpleDateFormat formatter = new SimpleDateFormat(dateformat8);
				dateString = formatter.format(date);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return dateString;
		}

		/**
		 * 获取当天日期
		 * @param format
		 * @return
		 */
		@SuppressWarnings("static-access")
		public static String getCurrentDay(String format) {
			String dateString = "";
			try {
				// 取时间
				Date date = new Date();
				Calendar calendar = new GregorianCalendar();
				calendar.setTime(date);
				// 把日期往后增加一天.整数往后推,负数往前移动
				calendar.add(calendar.DATE, 0);
				// 这个时间就是日期往后推一天的结果
				date = calendar.getTime();
				SimpleDateFormat formatter = new SimpleDateFormat(format);
				dateString = formatter.format(date);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return dateString;
		}

		/**
		 * 获取明天的日期(当天日期往后加一天)
		 * @param isTime
		 * @return
		 */
		@SuppressWarnings("static-access")
		public static String getNextDay(boolean isTime) {
			String dateString = "";
			try {
				// 取时间
				Date date = new Date();
				Calendar calendar = new GregorianCalendar();
				calendar.setTime(date);
				// 把日期往后增加一天.整数往后推,负数往前移动
				calendar.add(calendar.DATE, 1);
				// 这个时间就是日期往后推一天的结果
				date = calendar.getTime();
				SimpleDateFormat formatter = new SimpleDateFormat(dateformat8);
				dateString = formatter.format(date);
				if (isTime) {
					dateString += " 00:00:00";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return dateString;
		}

		/**
		 * 获取明天的日期(当天日期往后加一天)
		 * @param isTime
		 * @param timeStr
		 * @return
		 */
		@SuppressWarnings("static-access")
		public static String getNextDay(boolean isTime, String timeStr) {
			String dateString = "";
			try {
				// 取时间
				Date date = new Date();
				Calendar calendar = new GregorianCalendar();
				calendar.setTime(date);
				// 把日期往后增加一天.整数往后推,负数往前移动
				calendar.add(calendar.DATE, 1);
				// 这个时间就是日期往后推一天的结果
				date = calendar.getTime();
				SimpleDateFormat formatter = new SimpleDateFormat(dateformat8);
				dateString = formatter.format(date);
				if (isTime) {
					dateString += timeStr;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return dateString;
		}

		/**
		 * 获取明天的日期(当天日期往后加一天)
		 * @param isTime
		 * @param timeStr
		 * @param format
		 * @return
		 */
		@SuppressWarnings("static-access")
		public static String getNextDay(boolean isTime, String timeStr, String format) {
			String dateString = "";
			try {
				if (StringUtils.isNullOrEmpty(format)) {
					format = dateformat8;
				}
				// 取时间
				Date date = new Date();
				Calendar calendar = new GregorianCalendar();
				calendar.setTime(date);
				// 把日期往后增加一天.整数往后推,负数往前移动
				calendar.add(calendar.DATE, 1);
				// 这个时间就是日期往后推一天的结果
				date = calendar.getTime();
				SimpleDateFormat formatter = new SimpleDateFormat(format);
				dateString = formatter.format(date);
				if (isTime) {
					dateString += timeStr;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return dateString;
		}

		/**
		 * 获取当天日期前一天
		 * @param isTime
		 * @param format
		 * @return
		 */
		@SuppressWarnings("static-access")
		public static String getBeforeDay(boolean isTime, String format) {
			String dateString = "";
			try {
				if (StringUtils.isNullOrEmpty(format)) {
					format = dateformat8;
				}
				// 取时间
				Calendar calendar = Calendar.getInstance();
				// 把日期往后增加一天.整数往后推,负数往前移动
				calendar.add(calendar.DATE, -1);
				// 这个时间就是日期往后推一天的结果
				SimpleDateFormat formatter = new SimpleDateFormat(format);
				dateString = formatter.format(calendar.getTime());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return dateString;
		}
		
		/**
		 * 获取当天日期前一天
		 * @param amount	参数说明：时间
		 * @param isTime
		 * @param format
		 * @return
		 */
		@SuppressWarnings("static-access")
		public static String getTime(int amount, String format) {
			String dateString = "";
			try {
				if (StringUtils.isNullOrEmpty(format)) {
					format = dateformat8;
				}
				// 取时间
				Calendar calendar = Calendar.getInstance();
				// 把日期往后增加一天.整数往后推,负数往前移动
				calendar.add(calendar.DATE, amount);
				// 这个时间就是日期往后推一天的结果
				SimpleDateFormat formatter = new SimpleDateFormat(format);
				dateString = formatter.format(calendar.getTime());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return dateString;
		}
		
		/**
		 * 获取当天日期前一天
		 * @param amount	参数说明：时间
		 * @param isTime
		 * @param format
		 * @return
		 */
		@SuppressWarnings("static-access")
		public static String getTime(int amount, Date date, String format) {
			String dateString = "";
			try {
				if (StringUtils.isNullOrEmpty(format)) {
					format = dateformat8;
				}
				// 取时间
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				// 把日期往后增加一天.整数往后推,负数往前移动
				calendar.add(calendar.DATE, amount);
				// 这个时间就是日期往后推一天的结果
				SimpleDateFormat formatter = new SimpleDateFormat(format);
				dateString = formatter.format(calendar.getTime());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return dateString;
		}

		/**
		 * 时间之间的比较
		 * 
		 * @param beginTime
		 * @param endTime
		 * @return
		 */
		public static boolean getCompareTime(String beginTime, String endTime) {
			boolean timeFlag = false;
			Long compareTime1 = stringToDate(beginTime).getTime();
			Long compareTime2 = stringToDate(endTime).getTime();

			if (compareTime1 < compareTime2) {
				timeFlag = true;
			}
			return timeFlag;
		}

		/**
		 * 日期之间的比较
		 * 
		 * @param beginDay
		 * @param endDay
		 * @return
		 */
		public static boolean getCompareDay(String beginDay, String endDay) {
			boolean timeFlag = true;
			Long compareDay1 = stringToDate(beginDay, dateformat8).getTime();
			Long compareDay2 = stringToDate(endDay, dateformat8).getTime();

			if (compareDay1 < compareDay2) {
				timeFlag = false;
			}
			return timeFlag;
		}

		/**
		 * 获取当前时间的分钟数
		 * 
		 * @param formatStr
		 * @return
		 */
		public static String getMinuteTime() {
			Date date = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat13);
			// 其中yyyy-MM-dd是你要表示的格式
			String minute = dateFormat.format(date);
			// System.out.println("The date is : "+ minute);
			return minute;
		}

		/**
		 * 获取昨天的日期(当天日期往前减一天)
		 * 
		 * @param format
		 * @return
		 */
		@SuppressWarnings("static-access")
		public static String geYesterDay(String format) {
			String dateString = "";
			try {
				// 取时间
				Date date = new Date();
				Calendar calendar = new GregorianCalendar();
				calendar.setTime(date);
				// 把日期往后增加一天.整数往后推,负数往前移动
				calendar.add(calendar.DATE, -1);
				// 这个时间就是日期往后推一天的结果
				date = calendar.getTime();
				SimpleDateFormat formatter = new SimpleDateFormat(format);
				dateString = formatter.format(date);

			} catch (Exception e) {
				e.printStackTrace();
			}
			return dateString;
		}

		/**
		 * 返回当前年月日
		 * 
		 * @param format
		 * @return
		 */
		public static String getNowDate(String format) {
			if (StringUtils.isNullOrEmpty(format)) {
				format = dateformat14;
			}
			Date date = new Date();
			String nowDate = new SimpleDateFormat(format).format(date);
			return nowDate;
		}

		/**
		 * 返回当前星期几
		 * 
		 * @return
		 */
		public static String getWeekOfDate() {
			Date date = new Date();
			String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);

			int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
			if (w < 0)
				w = 0;

			return weekDays[w];
		}

		/**
		 * 将时间字符串“20140801:20140831”转换为-"2014.8.01 - 2014.8.31"
		 * 
		 * @param payDateStr
		 * @return
		 * @throws Exception
		 */
		public static String convertDateFormat(String payDateStr) {
			String payDate = "";
			try {
				if (!StringUtils.isNullOrEmpty(payDateStr)) {
					String[] dateStrings = payDateStr.split(":");
					SimpleDateFormat sdf = new SimpleDateFormat(dateformat);
					Date date = sdf.parse(dateStrings[0]);
					Date date2 = sdf.parse(dateStrings[1]);
					Calendar calendar = Calendar.getInstance();
					calendar.setTime(date);
					payDate = calendar.get(Calendar.YEAR) + "." + (calendar.get(Calendar.MONTH) + 1) + "."
							+ calendar.get(Calendar.DATE) + " - ";
					calendar.setTime(date2);
					payDate += calendar.get(Calendar.YEAR) + "." + (calendar.get(Calendar.MONTH) + 1) + "."
							+ calendar.get(Calendar.DATE);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return payDate;
		}

		/**
		 * 获取年月格式数据
		 * 
		 * @param month
		 * @param format
		 * @return
		 */
		@SuppressWarnings("static-access")
		public static String getYearMonth(int month, String format) {
			String yearMonth = "";
			try {
				// 取时间
				Date date = new Date();
				Calendar calendar = new GregorianCalendar();
				calendar.setTime(date);
				// 把日期往后增加一天.整数往后推,负数往前移动
				calendar.add(calendar.DATE, month);
				// 这个时间就是日期往后推一天的结果
				date = calendar.getTime();
				SimpleDateFormat formatter = new SimpleDateFormat(format);
				yearMonth = formatter.format(date);

				// System.out.println(dateString);

			} catch (Exception e) {
				e.printStackTrace();
			}
			return yearMonth;
		}

		/**
		 * 返回天数/时/分/秒
		 * 
		 * @param type
		 * @param time
		 * @param format
		 */
		public static long getDayHourMinuteSecond(String type, String time, String format) {
			long timeLong = 0;
			try {
				SimpleDateFormat df = new SimpleDateFormat(format);
				Date nowDate = df.parse(getNowDate(format));
				Date date = df.parse(time);
				long l = nowDate.getTime() - date.getTime();
				long day = l / (24 * 60 * 60 * 1000);
				long hour = (l / (60 * 60 * 1000) - day * 24);
				long minute = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
				long second = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60);

				if ("day".equals(type)) {
					timeLong = day;
				} else if ("hour".equals(type)) {
					timeLong = hour;
				} else if ("minute".equals(type)) {
					timeLong = minute;
				} else if ("second".equals(type)) {
					timeLong = second;
				}
				// System.out.println(""+day+"天"+hour+"小时"+minute+"分"+second+"秒");
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return timeLong;
		}

		/**
		 * 给出一个表示日期的字符串，返回一个日期类型的object
		 * 
		 * @param src
		 *            表示日期的字串
		 * @param df
		 *            字串对应的日期格式
		 * @return Date
		 */
		public static Date getDateByString(String src, String dateFormat) {
			try {
				return new SimpleDateFormat(dateFormat).parse(src);
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}

		/**
		 * 获取时间是上午还是下午
		 * 
		 * @param tempDate
		 * @param format
		 * @return
		 */
		public static String getDateAmOrPm() {
			String amPmStr = "";
			try {
				GregorianCalendar ca = new GregorianCalendar();
				int amPmInt = ca.get(GregorianCalendar.AM_PM);
				amPmStr = amPmInt == 0 ? "AM" : "PM";
			} catch (Exception ex) {
				amPmStr = "";
			}
			return amPmStr;
		}

		/**
		 * 获取本月时间
		 * 
		 * @return
		 */
		public static String getThisMonth(String type, String format) {
			String dateString = "";
			try {
				Calendar calendar = Calendar.getInstance();
				if ("begin".equals(type)) {
					calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
				} else if ("end".equals(type)) {
					calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
				}
				SimpleDateFormat formatter = new SimpleDateFormat(format);
				dateString = formatter.format(calendar.getTime());
			} catch (Exception ex) {
				dateString = "";
			}
			return dateString;
		}

		/**
		 * 获取上月时间
		 * 
		 * @return
		 */
		public static String getLaskMonth(String type) {
			int day = 30;
			String dateString = "";
			try {
				int year = 0;
				int maxDayOfMonth = 0;
				Calendar calendar = Calendar.getInstance();
				// 上个月月份
				int month = calendar.get(Calendar.MONTH);

				if (month == 0) {
					year = calendar.get(Calendar.YEAR) - 1;
					month = 12;
				} else {
					year = calendar.get(Calendar.YEAR);
				}

				int arrayTotalDayOfMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
				if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
					arrayTotalDayOfMonth[1] += 1;
				}
				for (int i = 0; i < 12; i++) {
					if (month == i + 1) {
						maxDayOfMonth = arrayTotalDayOfMonth[i];
						break;
					}
				}

				// 起始天数
				day = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
				String beginDay = year + "-" + month + "-" + day;
				String endDay = year + "-" + month + "-" + maxDayOfMonth;

				if ("begin".equals(type)) {
					dateString = beginDay;
				} else if ("end".equals(type)) {
					dateString = endDay;
				}
			} catch (Exception ex) {
				dateString = "";
			}
			return dateString;
		}

		/**
		 * 获取本周时间
		 * 
		 * @return
		 */
		public static String getThisWeek(String type, String format) {
			String dateString = "";
			try {
				Calendar calendar = Calendar.getInstance();
				if ("begin".equals(type)) {
					calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
				} else if ("end".equals(type)) {
					calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
					calendar.add(Calendar.WEEK_OF_YEAR, 1);
				}
				SimpleDateFormat formatter = new SimpleDateFormat(format);
				dateString = formatter.format(calendar.getTime());
			} catch (Exception ex) {
				dateString = "";
			}
			return dateString;
		}

		/**
		 * 获取上周时间
		 * 
		 * @return
		 */
		public static String getLaskWeek(int day, String format) {
			String dateString = "";
			try {
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
				int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
				int offset = day - dayOfWeek;
				calendar.add(Calendar.DATE, offset - 7);
				SimpleDateFormat formatter = new SimpleDateFormat(format);
				dateString = formatter.format(calendar.getTime());
			} catch (Exception ex) {
				dateString = "";
			}
			return dateString;
		}

		/**
		 * 根据自定义时间格式化转换long类型
		 * 
		 * @param dateFormat
		 * @return
		 */
		public static long getDateToLong(String dateFormat) {
			long longTime = 0;
			// 规定日期格式
			SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
			try {
				String dateStr = dateToString(new Date(), dateFormat);
				// 先转换成long型
				longTime = formatter.parse(dateStr).getTime();
			} catch (Exception ex) {
				longTime = 0;
			}
			return longTime;
		}

		/**
		 * 根据自定义时间格式化和字符串转换long类型
		 * 
		 * @param dateFormat
		 * @return
		 */
		public static long getStringToLong(String dateStr, String dateFormat) {
			long longTime = 0;
			// 规定日期格式
			SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
			try {
				// 先转换成long型
				longTime = formatter.parse(dateStr).getTime();
			} catch (Exception ex) {
				longTime = 0;
			}
			return longTime;
		}

		/**
		 * 根据日期获得所在周的日期 获取前天、今天、明天、后天等7天日期数据 例如:(8.23,8.24,8.25,8.26,8.27,8.28,8.29)
		 * 
		 * @param mdate
		 * @return
		 */
		@SuppressWarnings("deprecation")
		public static List<Map<String, Object>> dateToWeek(Date mdate, String dateFormat) {
			Date fdate;
			// Date currentDate = new Date();
			// 定义输出日期格式
			// SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
			int b = mdate.getDay();
			b = b > 2 || b < 2 ? 2 : b;
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			Long fTime = mdate.getTime() - b * 24 * 3600000;
			for (int a = 1; a <= 7; a++) {
				Map<String, Object> map = new LinkedHashMap<String, Object>();
				fdate = new Date();
				fdate.setTime(fTime + (a * 24 * 3600000));
				map.put("DAYS_NUM", a);
				map.put("DATE_WEEK", dateToString(fdate, dateFormat));
				list.add(map);
			}
			return list;
		}

		/**
		 * 获取当月第一天或最后一天
		 * 
		 * @param type
		 * @return
		 */
		public static String getMonthFirstDayOrLastDay(String type) {
			SimpleDateFormat format = new SimpleDateFormat(dateformat8);
			Calendar calendar = Calendar.getInstance();
			if ("first".equals(type)) {
				calendar.add(Calendar.MONTH, 0);
				// 设置为1号,当前日期既为本月第一天
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				return format.format(calendar.getTime());
			} else if ("last".equals(type)) {
				// 获取当前月最后一天
				calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
				return format.format(calendar.getTime());
			}
			return "";
		}

		/**
		 * 获取当月第一天或最后一天
		 * 
		 * @param type
		 * @param lastAddDay
		 * @return
		 */
		public static String getMonthFirstDayOrLastDay(String type, int lastAddDay) {
			SimpleDateFormat format = new SimpleDateFormat(dateformat8);
			Calendar calendar = Calendar.getInstance();
			if ("first".equals(type)) {
				calendar.add(Calendar.MONTH, 0);
				// 设置为1号,当前日期既为本月第一天
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				return format.format(calendar.getTime());
			} else if ("last".equals(type)) {
				// 获取当前月最后一天
				calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH) + lastAddDay);
				return format.format(calendar.getTime());
			}
			return "";
		}

		/**
		 * 把Date转换成String，以yyyy-MM-dd HH:mm:ss的形式显示
		 * 
		 * @param tempDate
		 * @return
		 */
		public static String dateToStringFormatByLongTime(long tempDateStr) {
			String date_str = "";
			SimpleDateFormat formatter = new SimpleDateFormat(dateformat5); // 规定日期格式
			try {
				Date tempDate = new Date(tempDateStr);
				date_str = formatter.format(tempDate);
			} catch (Exception ex) {
				date_str = "";
			}
			return date_str;
		}

		/**
		 * 把Date转换成String，以yyyy-MM-dd HH:mm:ss的形式显示
		 * 
		 * @param tempDate
		 * @param dateformat
		 * @return
		 */
		public static String dateToStringFormatByLongTime(long tempDateStr, String dateformat) {
			String date_str = "";
			// 规定日期格式
			SimpleDateFormat formatter = new SimpleDateFormat(dateformat);
			try {
				Date tempDate = new Date(tempDateStr);
				date_str = formatter.format(tempDate);
			} catch (Exception ex) {
				date_str = "";
			}
			return date_str;
		}

		public static void main(String[] args) {
//			List<String> list = new ArrayList<String>();
//			list.add("gz|20171227100029|3|2|PldeaYlRSBAUq2nZzGSjKg==|460030942851917|0|020|0881|020|2|1475|2|5C32|1|0|0|0|0|0|0|");
//			list.add("20171226032523");
//			list.add("20171226012523");
//			String timer = "20180122173751";
//			String date = "2017-12-21 09:18:00";
//			System.out.println(dateToStringFormat(timer,dateformat4,dateformat5));
//			System.out.println(dateToStringFormat(dateToStringFormat(timer,dateformat4,dateformat5), DateUtils.dateformat5, DateUtils.dateformat15));
//			System.out.println(date.split(" ")[1].split(":")[1]);
//			
//			String beforeDateTime = DateUtils.BeforedateToString(DateUtils.dateToString(new Date()),DateUtils.dateformat5,1);
//			System.out.println(beforeDateTime);
//			System.out.println(dateToStringFormat(beforeDateTime,dateformat5,dateformat));
//			System.out.println(list.contains(dateToStringFormat(beforeDateTime,dateformat5,dateformat)));
//			System.out.println(list.toString().contains(dateToStringFormat(beforeDateTime,dateformat5,dateformat)));
//			
//			System.out.println(dateToStringFormat(beforeDateTime,dateformat5,dateformat));
//			System.out.println(dateToStringFormat(date,dateformat5,dateformat16));
			
//			System.out.println(DateUtils.dateToStringFormat("20180416104558", DateUtils.dateformat4, DateUtils.dateformat));
			
			System.out.println(timestampToDate(1526960720087L, dateformat8));
			System.out.println(dateToTimestamp("2018-05-02 00:00:00"));
			System.out.println(getCurrentDay());
		}
}
